package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.CarPortDto;
import io.geekidea.boot.test.entity.CarPort;
import io.geekidea.boot.test.mapper.CarPortMapper;
import io.geekidea.boot.test.query.CarPortQuery;
import io.geekidea.boot.test.service.CarPortService;
import io.geekidea.boot.test.vo.CarPortVo;
import io.geekidea.boot.test.query.AppCarPortQuery;
import io.geekidea.boot.test.vo.AppCarPortVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 车库 服务实现类
 *
 * @author geekidea
 * @since 2025-03-25
 */
@Slf4j
@Service
public class CarPortServiceImpl extends ServiceImpl<CarPortMapper, CarPort> implements CarPortService {

    @Autowired
    private CarPortMapper carPortMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCarPort(CarPortDto dto) {
        CarPort carPort = new CarPort();
        BeanUtils.copyProperties(dto, carPort);
        return save(carPort);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCarPort(CarPortDto dto) {
        Long id = dto.getId();
        CarPort carPort = getById(id);
        if (carPort == null) {
            throw new BusinessException("车库不存在");
        }
        BeanUtils.copyProperties(dto, carPort);
        return updateById(carPort);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCarPort(Long id) {
        return removeById(id);
    }

    @Override
    public CarPortVo getCarPortById(Long id) {
        return carPortMapper.getCarPortById(id);
    }

    @Override
    public Paging<CarPortVo> getCarPortPage(CarPortQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<CarPortVo> list = carPortMapper.getCarPortPage(query);
        Paging<CarPortVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppCarPortVo getAppCarPortById(Long id) {
        return carPortMapper.getAppCarPortById(id);
    }

    @Override
    public Paging<AppCarPortVo> getAppCarPortPage(AppCarPortQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppCarPortVo> list = carPortMapper.getAppCarPortPage(query);
        Paging<AppCarPortVo> paging = new Paging<>(list);
        return paging;
    }

}
